new Vue({
    el: "#container",
    data() {
        return {
            // 查询数据
            initIndentId: null,
            tableSearch: {
                pageIndex: 0,
                limit: 50,
                total: 0,
                dateFormat: "yyyy-MM-dd HH:mm",
                startTime: null,
                endTime: null,
                conName: "",
                site: "",
                indentId: null,
                invoiceCode: null,
                indentCode: null,
                isAdd: -1,
            },
            //  表数据
            tableDatas: [],
            tableLoading: false,
            //  详情数据
            detailDialog: {
                show: false,
                invoiceId: false,
                loading: false,
            },
            //  消耗记录
            invExpendDialog: {
                show: false,
                id: null,
            },
            invExpendActiveName: '',
            invoice: {},
            batchExpends: [],
            expendDialog: {
                show: false,
                loading: false,
            },
            invoiceDetail: {},
        }
    },
    // 在模板渲染成html前调用
    created() {
        var that = this;
        that.loadTableDatas();
    },
    // 在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
    mounted() {
        var that = this;
    },
    methods: {
        searchDatas() {
            let that = this;
            that.tableSearch.pageIndex = 0;
            that.tableSearch.limit = 50;
            that.loadTableDatas();
        },
        // 加载数据
        loadTableDatas: function () {
            let that = this;
            that.tableLoading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/page",
                async: true,
                dataType: 'json',
                data: that.tableSearch,
                success: function (res) {
                    if (res.code == 0) {
                        let page = res.data;
                        that.tableDatas = page.records;
                        that.tableSearch.total = page.total;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.tableLoading = false;
                }
            })
        },
        /**
         * 查询发货单的消耗记录
         * @param invoice
         */
        showExpendDialog(invoice) {
            let that = this;
            that.invExpendDialog.show = true;
            let invId = invoice.id;
            that.invoice = invoice;
            that.invExpendDialog.invoice = invoice;
            //  加载消耗
            if (isNull(invId)) {
                that.$message({
                    showClose: true,
                    message: '请选择正确的发货单',
                    type: 'error'
                });
                return;
            }
            that.expendDialog.show = true;
            that.expendDialog.loading = true;
            $.ajax({
                type: "post",
                url: "/ins/lab-expend/invoice/expend/" + invId,
                async: true,
                dataType: 'json',
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        let batchExpends = res.data;
                        if (batchExpends == null || batchExpends.length == 0) {
                            that.$message({
                                showClose: true,
                                message: '该发货单没有消耗记录',
                            });
                            return;
                        }
                        that.batchExpends = batchExpends;
                        that.invExpendActiveName = batchExpends[0].batchNum;
                    } else {
                        that.$message({
                            showClose: true,
                            message: '查询发货单消耗数据失败：' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.expendDialog.loading = false;
                }
            })
        },
        /**
         * 显示发货单详情
         * @param invoiceId
         */
        showInvDetail(invoiceId) {
            let that = this;
            that.detailDialog.show = true;
            //  加载数据
            that.detailDialog.loading = true;
            $.ajax({
                type: "POST",
                url: "/pro/invoice/detail/" + invoiceId,
                async: true,
                dataType: 'json',
                success: function (res) {
                    if (res.code == 0) {
                        // console.log(res.data);
                        that.invoiceDetail = res.data;
                    } else {
                        that.$message({
                            showClose: true,
                            message: "查询发货单详情失败:" + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    that.$message({
                        showClose: true,
                        message: '请求失败，请联系管理员',
                        type: 'error'
                    });
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                    that.detailDialog.loading = false;
                }
            });
        },
        printInvoice(invoiceId) {
            let that = this;
            $.ajax({
                type: "POST",
                url: "/pro/dispatch/print/" + invoiceId,
                async: true,
                data: {},
                success: function (res) {
                    if (res.code == 0) {
                        that.$message({
                            showClose: true,
                            message: '开始打印',
                            type: 'success'
                        });
                    } else {
                        that.$message({
                            showClose: true,
                            message: '打印失败:' + res.msg,
                            type: 'error'
                        });
                    }
                },
                error: function (res) {
                    return;
                },
                complete: function (XMLHttpRequest, status) {
                }
            })
        },
        // 修改每页数据
        handleSizeChange(val) {
            var that = this;
            that.tableSearch.limit = val;
            // 查询数据
            that.loadTableDatas();
        },
        handleCurrentChange(val) {
            // 设置页数
            var that = this;
            that.tableSearch.pageIndex = val;
            that.loadTableDatas();
        },
        handleClick(tab, event) {
            /*let that = this;
            let index = tab.index;
            let dayValue = that.dayValues[index];
            //  销售人员名
            let saleMan = dayValue.sheetName;*/
        },
        //  计算单方设定用量
        initUnitValue(formulaQuantity, batchStere) {
            let that = this;
            let t = formulaQuantity / batchStere;
            t = parseFloat(formulaQuantity / batchStere).toFixed(2);
            return t;
        },
    }
})
